<?php

/* SVN FILE: $Id: generic_controller.php 116 2007-04-16 21:10:00Z Andy $ */

/**
 * Short description for file.php.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * Copyright (c), Andy Dawson
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @filesource
 * @copyright    Copyright (c) 2007, Andy Dawson
 * @package      noswad
 * @subpackage   app.plugins.pagination
 * @since        Noswad site version 3
 * @todo         TODO Edit this block
 * @version      $Revision: 116 $
 * @created      26/01/2007
 * @modifiedby   $LastChangedBy$
 * @lastmodified $Date: 2007-04-16 23:10:00 +0200 (lun, 16 abr 2007) $
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Short description for Class
 *
 * Long descrption of Class
 */

/**
 * Description for var
 *
 * @var type
 * @access public/private/protected
 */

/**
 * Description for method
 *
 * @param type $name description
 * @return type description
 */
if (!defined('CAKE_ADMIN')) {
	define('CAKE_ADMIN', 'adminString_SomethingYouDefine');
}
include ('error.php');
class GenericController extends AppController {
	var $components= array (
		'Menu',
		'RequestHandler'
	);
	var $helpers= array (
		'Form',
		'Ajax',
		'Javascript',
		'Time',
		'Menu'
	);
	var $javascripts= array ();
	var $layout= 'noswad';
	var $menuActions= array ();

	function __construct() {
		// Inherit Parent controller vars if not direct child of App controller
		$this->__inheritVars(get_class($this));
		parent :: __construct();
	}

	// Recursively inherit vars
	function __inheritVars($className) {
		if (low($className) <> ('appcontroller')) {
			$ParentClass= get_parent_class($className);
			$ParentVars= get_class_vars($ParentClass);
			foreach (array (
					'components',
					'helpers',
					'uses'
				) as $var) {
				if (isset ($ParentVars[$var]) && !empty ($ParentVars[$var]) && is_array($this-> {
					$var })) {
					$diff= array_diff($ParentVars[$var], $this-> {
						$var });
					$this-> {
						$var }
					= array_merge($this-> {
						$var }, $diff);
				}
			}
			$this->__inheritVars($ParentClass);
		}
	}

	// Just manages the menu. Delete unless using the layout that comes with the download
	function beforeRender() {
		if (!isset ($this->viewVars['data'])) {
			$this->set('data', $this->data);
			$this->params['data'] = $this->data;
		}
		if (isset($this->Menu) && !isset ($this->params['requested']) && !$this->RequestHandler->isAjax()) {
			$this->Menu->setVars();
		}
		//$this->set('javascripts', $this->javascripts);
	}

	// Allows the possibility to have more than one flash message.
	function _addFlash($flashMessage, $layout= 'flash_info', $params= array (), $key= 'flash') {
		if (isset($this->Session)) {
			uses('Sanitize');
			// Prevent duplicate flash messages, by using the message as the key
			$i= Sanitize :: paranoid($flashMessage);
			$this->Session->setFlash($flashMessage, $layout, $params, $key . $i);
		}
	}

	// Ultimate redirect function.
	function redirect($url, $status= null, $die= true) {
		if ($this->RequestHandler->isAjax() || isset ($this->params['requested'])) {
			$data= $this->requestAction($url, array (
				'return'
			));
			$this->set('data', $data);
			$this->viewPath= '_generic';
			$this->render('ajax_redirect');
		} else {
			if (is_array($url)) {
				$urls = '/';
				if (defined('CAKE_ADMIN')&&isset($url[CAKE_ADMIN])&&$url[CAKE_ADMIN]) {
					$urls .= CAKE_ADMIN.'/';
					unset($url[CAKE_ADMIN]);
				}
				if (isset($url['plugin'])&&$url['plugin']) {
					$urls .= $url['plugin'].'/';
					unset($url['plugin']);
				} elseif ($this->plugin) {
					$urls .= $this->plugin.'/';
				}
				if (isset($url['controller'])) {
					$urls .= $url['controller'].'/';
					unset($url['controller']);
				} else {
					$urls .= $this->name.'/';
				}
				if (isset($url['action'])) {
					$urls .= $url['action'].'/';
					unset($url['action']);
				} else {
					$urls .= $this->action.'/';
				}
				if (isset($url['#'])) {
					$end = '#'.$url['#'];
					unset($url['#']);
				} else {
					$end = '';
				}
				if (isset($url['?'])) {
					$end .= '?'.$url['?'];
					unset($url['?']);
				}
				foreach ($url as $key => $val) {
					if (is_numeric($key)) {
						$urls .= $val.'/';
					} else {
						$urls .= $key.':'.$val.'/';
					}
				}
				$url = $urls.$end;
			}
			parent :: redirect($url, $status);
		}
		if ($die) {
			die();
		} else {
			return false;
		}
	}

	function css() {
		$db= & ConnectionManager :: getDataSource('default');
		$db->fullDebug= false;
		$this->autoLayout= false;
		$this->viewPath= 'webroot/css';
		header('Content-type: text/css');
		$this->_cacheMe();

		$this->render(join(DS, $this->params['pass']));
		Configure :: write('debug', 0);
	}
	function js() {
		$db= & ConnectionManager :: getDataSource('default');
		$db->fullDebug= false;
		$this->autoLayout= false;
		$this->viewPath= 'webroot/js';
		$this->_cacheMe();
		header('Content-type: text/javascript');
		$this->render(join(DS, $this->params['pass']));
		Configure :: write('debug', 0);
	}

	function _cacheMe() {
		header('Date: ' . date('D, j M Y G:i:s ') . 'GMT');
		header('Content-Type: text/css');
		header('Expires: ' . gmdate('D, j M Y H:i:s', time() + YEAR) . ' GMT');
		header('Cache-Control: cache'); // HTTP/1.1
		header('Pragma: cache'); // HTTP/1.0
	}
}
?>